<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>添加管理员</title>
<%@include file="/WEB-INF/admin/head.jsp"%>
<%@include file="/WEB-INF/admin/common.jsp"%>
</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="myForm">
			<div class="layui-form-item">
				<label for="username" class="layui-form-label"> <span class="x-red">*</span>登录名
				</label>
				<div class="layui-input-inline">
					
					<input type="text" id="username" name="username" required="" lay-verify="required" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">
					<span class="x-red">*</span>将会成为您唯一的登入名
				</div>
			</div>

			
			<div class="layui-form-item">
				<label for="phone" class="layui-form-label"> <span class="x-red">*</span>手机
				</label>
				<div class="layui-input-inline">
					<input type="text" id="phone" name="phone" required="" lay-verify="phone" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">
					<span class="x-red">*</span>将会成为您唯一的登入名
				</div>
			</div>
			<div class="layui-form-item">
				<label for="L_email" class="layui-form-label"> <span class="x-red">*</span>邮箱
				</label>
				<div class="layui-input-inline">
					<input type="text" id="L_email" name="email" required="" lay-verify="email" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">
					<span class="x-red">*</span>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="x-red">*</span>角色</label>
				<div class="layui-input-block">
					<input type="checkbox" name="like1[write]" lay-skin="primary" title="超级管理员" checked=""> <input type="checkbox" name="like1[read]"
						lay-skin="primary" title="编辑人员"> <input type="checkbox" name="like1[write]" lay-skin="primary" title="宣传人员" checked="">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="L_pass" class="layui-form-label"> <span class="x-red">*</span>密码
				</label>
				<div class="layui-input-inline">
					<input type="password" id="L_password" name="password" required="" lay-verify="password" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">6到16个字符</div>
			</div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> <span class="x-red">*</span>确认密码
				</label>
				<div class="layui-input-inline">
					<input type="password" id="L_repass" name="repass" required="" lay-verify="repass" autocomplete="off" class="layui-input">
				</div>
			</div>


			<div class="layui-form-item">
				<label for="sort" class="layui-form-label"> <span class="x-red">*</span>排序升序
				</label>
				<div class="layui-input-inline">
					<input type="text" id="sort" name="sort" required="" value="100" lay-verify="required" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">
					<span class="x-red">*</span>
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label"><span class="x-red">*</span>状态</label>
				<div class="layui-input-block">
					<input type="radio" name="status" lay-skin="primary" value="0" title="正常" checked=""> <input type="radio" name="status"
						lay-skin="primary" value="1" title="冻结">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label  class="layui-form-label"><span class="x-red">*</span>图片</label>
				<div class="layui-input-block">
				<input type="hidden" id="avatar" name="avatar" value="" required=""  lay-verify="required" autocomplete="off" class="layui-input">
					<div class="layui-upload">
						<button type="button" class="layui-btn" id="uploadImg">上传图片</button>
						<div class="layui-upload-list">
							<img class="layui-upload-img" id="preImg" width="100" heigth="100">
							<p id="demoText"></p>
						</div>
					</div>
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">操作</label>
				<div class="layui-input-block">
					<button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
				</div>
			</div>
		</form>
	</div>
	<script>
		layui.use([ 'form', 'layer','upload' ],function() {
			var $ = layui.jquery;
			var form = layui.form, layer = layui.layer;
			var upload = layui.upload;
			//普通图片上传
			var uploadInst = upload.render({
					elem : '#uploadImg',
					url : '${basePath }/admin/uploadImg.do',
					field : "image",
					before : function(obj) {
					//预读本地文件示例，不支持ie8
						obj.preview(function(index, file,result) {
							$('#preImg').attr('src', result); //图片链接（base64）
						});
					},
					done : function(res) {
						var url=res.data.url;
						//如果上传失败
						if (res.code > 0) {
							return layer.msg('上传失败');
						} else {
							form.val("myForm",{
								  "avatar":url
							})
						}
						//上传成功
					},
					error : function() {
						  var demoText = $('#demoText');
					      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
					      demoText.find('.demo-reload').on('click', function(){
					        uploadInst.upload();
					      });
					}
				});

				//自定义验证规则
				form.verify({
					nikename : function(value) {
						if (value.length < 5) {
							return '昵称至少得5个字符啊';
						}
					},
					password : [ /(.+){6,12}$/, '密码必须6到12位' ],
					repass : function(value) {
						if ($('#L_password').val() != $('#L_repass')
								.val()) {
							return '两次密码不一致';
						}
					}
				});

				//监听提交
				form.on('submit(add)', function(data) {
					console.log(data);
					$.ajax({
			            type: "POST",
			            url: "${basePath }/admin/member/addMemberSave.do",
			            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
			     		data:data.field,
			            success: function(res){
			            	console.log(res);
			            	if(res.code==0){
			            		//发异步，把数据提交给php
								layer.alert("添加成功", {icon : 6}, function() {
									// 获得frame索引
									var index = parent.layer.getFrameIndex(window.name);
									//关闭当前frame
									parent.layer.close(index);
									// 可以对父窗口进行刷新 
									x_admin_father_reload();
								});
			            	}else{
			            		layer.alert(res.msg, {icon : 1});
			            	}
				        }
					})
					
					
					return false;
				});

	});
	</script>

</body>

</html>